package com.jianzh5.action.admin;

import com.jianzh5.action.BaseAction;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.service.AskServiceI;
import com.jianzh5.util.InfoCenterHome;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * <code>AskAction</code>
 * </p>
 *
 * @author jianzhang11
 * @version 1.0
 * @desc
 * @Date: 2016/9/28 10:59
 * @since 1.0
 */
@Controller
@RequestMapping("/cms/ask")
public class AskAction extends BaseAction{
    @Autowired
    private AskServiceI askService;

    @RequestMapping(value = "/askList" ,method = RequestMethod.GET)
    @RequiresPermissions("cms:ask:view")
    public String askList(){
        return InfoCenterHome.SERVLET_ADMIN_ROOT+"cms/askList";
    }

    @RequestMapping(value = "/askList",method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("cms:ask:view")
    public DataTables askList(DataPage datapage, HttpServletRequest request){
        String sindex = request.getParameter("order[0][column]");
        String order = request.getParameter("order[0][dir]");
        String sort = request.getParameter("columns["+sindex+"][data]");
        DataTables dt = askService.dataTables(order,sort,datapage);
        return dt;
    }

    @ResponseBody
    @RequestMapping("/askDel")
    @RequiresPermissions("cms:ask:del")
    public ResultJson askDel(@RequestParam("aid") int aid){
        ResultJson json = new ResultJson();
        try{
            askService.delAsk(aid);
            json.setSuccess(true);
            json.setMsg("留言刪除成功...");
        }catch (Exception e) {
            e.printStackTrace();
//            logger.error("删除留言操作异常",e);
            json.setMsg("留言刪除失败,数据库异常...");
        }
        return json;
    }

}
